home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SPINDIAL.PAK / SPINCTL.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  8KB  |  293 lines

  1. // spinctl.cpp : Implementation of the CSpindialCtrl OLE control class.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include "spindial.h"
  16. #include "spinctl.h"
  17. #include "spinppg.h"
  18.  
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char BASED_CODE THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // Numeric constants
  28.  
  29. const MAX_POSITIONS = 4;
  30. const TICK_LEN = 6;
  31.  
  32.  
  33. IMPLEMENT_DYNCREATE(CSpindialCtrl, COleControl)
  34.  
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // Message map
  38.  
  39. BEGIN_MESSAGE_MAP(CSpindialCtrl, COleControl)
  40.     //{{AFX_MSG_MAP(CSpindialCtrl)
  41.     ON_WM_LBUTTONDOWN()
  42.     //}}AFX_MSG_MAP
  43.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  44. END_MESSAGE_MAP()
  45.  
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // Dispatch map
  49.  
  50. BEGIN_DISPATCH_MAP(CSpindialCtrl, COleControl)
  51.     //{{AFX_DISPATCH_MAP(CSpindialCtrl)
  52.     DISP_PROPERTY_EX(CSpindialCtrl, "NeedlePosition", GetNeedlePosition, SetNeedlePosition, VT_I2)
  53.     DISP_DEFVALUE(CSpindialCtrl, "NeedlePosition")
  54.     //}}AFX_DISPATCH_MAP
  55.     DISP_FUNCTION_ID(CSpindialCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  56. END_DISPATCH_MAP()
  57.  
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // Event map
  61.  
  62. BEGIN_EVENT_MAP(CSpindialCtrl, COleControl)
  63.     //{{AFX_EVENT_MAP(CSpindialCtrl)
  64.     EVENT_CUSTOM("SpinPositive", FireSpinPositive, VTS_NONE)
  65.     //}}AFX_EVENT_MAP
  66. END_EVENT_MAP()
  67.  
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // Property pages
  71.  
  72. // TODO: Add more property pages as needed.  Remember to increase the count!
  73. BEGIN_PROPPAGEIDS(CSpindialCtrl, 1)
  74.     PROPPAGEID(CSpindialPropPage::guid)
  75. END_PROPPAGEIDS(CSpindialCtrl)
  76.  
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // Initialize class factory and guid
  80.  
  81. IMPLEMENT_OLECREATE_EX(CSpindialCtrl, "SPINDIAL.SpindialCtrl.1",
  82.     0x6889605, 0xb8d0, 0x101a, 0x91, 0xf1, 0x0, 0x60, 0x8c, 0xea, 0xd5, 0xb3)
  83.  
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // Type library ID and version
  87.  
  88. IMPLEMENT_OLETYPELIB(CSpindialCtrl, _tlid, _wVerMajor, _wVerMinor)
  89.  
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. // Interface IDs
  93.  
  94. const IID BASED_CODE IID_DSpindial =
  95.         { 0x37446b89, 0x5870, 0x101b, { 0xb5, 0x7b, 0x0, 0x60, 0x8c, 0xc9, 0x6a, 0xfa } };
  96. const IID BASED_CODE IID_DSpindialEvents =
  97.         { 0x37446b8a, 0x5870, 0x101b, { 0xb5, 0x7b, 0x0, 0x60, 0x8c, 0xc9, 0x6a, 0xfa } };
  98.  
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // Control type information
  102.  
  103. static const DWORD BASED_CODE _dwSpindialOleMisc =
  104.     OLEMISC_ACTIVATEWHENVISIBLE |
  105.     OLEMISC_SETCLIENTSITEFIRST |
  106.     OLEMISC_INSIDEOUT |
  107.     OLEMISC_CANTLINKINSIDE |
  108.     OLEMISC_RECOMPOSEONRESIZE;
  109.  
  110. IMPLEMENT_OLECTLTYPE(CSpindialCtrl, IDS_SPINDIAL, _dwSpindialOleMisc)
  111.  
  112.  
  113. /////////////////////////////////////////////////////////////////////////////
  114. // CSpindialCtrl::CSpindialCtrlFactory::UpdateRegistry -
  115. // Adds or removes system registry entries for CSpindialCtrl
  116.  
  117. BOOL CSpindialCtrl::CSpindialCtrlFactory::UpdateRegistry(BOOL bRegister)
  118. {
  119.     if (bRegister)
  120.         return AfxOleRegisterControlClass(
  121.             AfxGetInstanceHandle(),
  122.             m_clsid,
  123.             m_lpszProgID,
  124.             IDS_SPINDIAL,
  125.             IDB_SPINDIAL,
  126.             FALSE,                      //  Not insertable
  127.             _dwSpindialOleMisc,
  128.             _tlid,
  129.             _wVerMajor,
  130.             _wVerMinor);
  131.     else
  132.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  133. }
  134.  
  135.  
  136. /////////////////////////////////////////////////////////////////////////////
  137. // CSpindialCtrl::CSpindialCtrl - Constructor
  138.  
  139. CSpindialCtrl::CSpindialCtrl()
  140. {
  141.     InitializeIIDs(&IID_DSpindial, &IID_DSpindialEvents);
  142. }
  143.  
  144.  
  145. /////////////////////////////////////////////////////////////////////////////
  146. // CSpindialCtrl::~CSpindialCtrl - Destructor
  147.  
  148. CSpindialCtrl::~CSpindialCtrl()
  149. {
  150. }
  151.  
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // CSpindialCtrl::OnDraw - Drawing function
  155.  
  156. void CSpindialCtrl::OnDraw(
  157.             CDC* pdc, const CRect& rcBounds, const CRect&)
  158. {
  159.     LPRECT      lpr;
  160.     OLE_COLOR   color;
  161.     CBrush      br;
  162.     POINT       centerPt;
  163.     POINT       tickpos[MAX_POSITIONS];
  164.     CPen* pOldPen;
  165.  
  166.     lpr = (LPRECT)(LPCRECT)rcBounds;
  167.  
  168.     // Paint background using the ambient background color
  169.     color = AmbientBackColor();
  170.     br.CreateSolidBrush(TranslateColor(color));
  171.     pdc->FillRect(lpr, &br);
  172.  
  173.     // Draw the dial using a black pen
  174.     pOldPen = (CPen*)pdc->SelectStockObject(BLACK_PEN);
  175.     pdc->Ellipse(lpr);
  176.  
  177.     // Determine coordinates of center point
  178.     centerPt.x = lpr->left + ((lpr->right - lpr->left) / 2);
  179.     centerPt.y = lpr->top + ((lpr->bottom - lpr->top) / 2);
  180.  
  181.     // Determine coordinates of intersections of tick marks and dial
  182.     tickpos[0].x = centerPt.x;
  183.     tickpos[0].y = lpr->top;
  184.     tickpos[1].x = lpr->right;
  185.     tickpos[1].y = centerPt.y;
  186.     tickpos[2].x = centerPt.x;
  187.     tickpos[2].y = lpr->bottom;
  188.     tickpos[3].x = lpr->left;
  189.     tickpos[3].y = centerPt.y;
  190.  
  191.     // Draw each tick mark
  192.     pdc->MoveTo(tickpos[0]);
  193.     pdc->LineTo(tickpos[0].x, tickpos[0].y+TICK_LEN);
  194.     pdc->MoveTo(tickpos[1]);
  195.     pdc->LineTo(tickpos[1].x-TICK_LEN, tickpos[1].y);
  196.     pdc->MoveTo(tickpos[2]);
  197.     pdc->LineTo(tickpos[2].x,tickpos[2].y-TICK_LEN);
  198.     pdc->MoveTo(tickpos[3]);
  199.     pdc->LineTo(tickpos[3].x+TICK_LEN, tickpos[3].y);
  200.  
  201.     // Draw pointer of dial
  202.     pdc->MoveTo(centerPt);
  203.     pdc->LineTo(tickpos[m_needlePosition]);
  204.  
  205.     pdc->SelectObject(pOldPen);
  206.  
  207.     return;
  208. }
  209.  
  210.  
  211. /////////////////////////////////////////////////////////////////////////////
  212. // CSpindialCtrl::DoPropExchange - Persistence support
  213.  
  214. void CSpindialCtrl::DoPropExchange(CPropExchange* pPX)
  215. {
  216.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  217.     COleControl::DoPropExchange(pPX);
  218.  
  219.     PX_Short(pPX, _T("NeedlePosition"), m_needlePosition, 0);
  220. }
  221.  
  222.  
  223. /////////////////////////////////////////////////////////////////////////////
  224. // CSpindialCtrl::OnResetState - Reset control to default state
  225.  
  226. void CSpindialCtrl::OnResetState()
  227. {
  228.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  229. }
  230.  
  231.  
  232. /////////////////////////////////////////////////////////////////////////////
  233. // CSpindialCtrl::AboutBox - Display an "About" box to the user
  234.  
  235. void CSpindialCtrl::AboutBox()
  236. {
  237.     CDialog dlgAbout(IDD_ABOUTBOX_SPINDIAL);
  238.     dlgAbout.DoModal();
  239. }
  240.  
  241.  
  242. /////////////////////////////////////////////////////////////////////////////
  243. // CSpindialCtrl::GetNeedlePosition - Return needle position.
  244.  
  245. short CSpindialCtrl::GetNeedlePosition()
  246. {
  247.     return m_needlePosition;
  248. }
  249.  
  250.  
  251. /////////////////////////////////////////////////////////////////////////////
  252. // CSpindialCtrl::SetNeedlePosition - Set needle position.
  253.  
  254. void CSpindialCtrl::SetNeedlePosition(short nNewValue)
  255. {
  256.     // Constrain value to 0..3 range
  257.     if ( (nNewValue > 3) || (nNewValue < 0) )
  258.         m_needlePosition = 0;
  259.     else
  260.         m_needlePosition = nNewValue;
  261.  
  262.     SetModifiedFlag(TRUE);
  263.  
  264.     InvalidateControl();
  265. }
  266.  
  267.  
  268. /////////////////////////////////////////////////////////////////////////////
  269. // CSpindialCtrl::OnLButtonDown - Spin needle and fire event.
  270.  
  271. void CSpindialCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  272. {
  273.     //  Spin needle position
  274.     SpinNeedlePosition();
  275.  
  276.     // Fire "spin positive" event
  277.     FireSpinPositive();
  278.  
  279.     COleControl::OnLButtonDown(nFlags, point);
  280. }
  281.  
  282.  
  283. /////////////////////////////////////////////////////////////////////////////
  284. // CSpindialCtrl::SpinNeedlePosition - Spin needle to next position.
  285.  
  286. void CSpindialCtrl::SpinNeedlePosition()
  287. {
  288.      short np = GetNeedlePosition();
  289.      if (np == 3) np = 0;
  290.          else np++;
  291.      SetNeedlePosition(np);
  292. }
  293.